package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
<<<<<<< HEAD
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.WmNewsMapper;
=======
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
>>>>>>> d85f04899912ec8431d04fc95699de5f6201f34b
import com.heima.admin.service.AdminService;
import com.heima.admin.service.WmNewsService;
import com.heima.admin.service.WmUserService;
import com.heima.model.admin.dtos.RequestParamDto;
<<<<<<< HEAD
import com.heima.model.admin.pojos.WmNews;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
=======
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
>>>>>>> d85f04899912ec8431d04fc95699de5f6201f34b
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@Transactional
<<<<<<< HEAD
public abstract class AdminServiceImpl  extends ServiceImpl<WmNewsMapper, WmNews> implements AdminService {
=======
public class AdminServiceImpl implements AdminService {
    @Autowired
    private WmNewsService wmNewsService;

    @Autowired
    private WmUserService wmUserService;

>>>>>>> d85f04899912ec8431d04fc95699de5f6201f34b
    /**
     * 查询文章列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getList(RequestParamDto dto) {
<<<<<<< HEAD
        //1.检查分页参数(防止错误分页参数,参数错误便重置为默认值)
        dto.checkParam();
        //2.检查请求参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //3.进行分页条件查询
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        //4.按照标题进行模糊查询
        if (StringUtils.isNotBlank(dto.getTitle())){
            wrapper.like(WmNews::getTitle,dto.getTitle());
        }
        //5.按照文章审核状态进行精确查询
        if (dto.getStatus()!=null){
            wrapper.eq(WmNews::getStatus,dto.getStatus());
        }
        //6.文章查询按照创建时间进行倒序排序
        wrapper.orderByDesc(WmNews::getCreatedTime);

        //进行文章列表查询
        page = page(page,wrapper);

        //结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());

=======
        // 1.检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        Page<WmNews> pageInfo = null;
        // 2.分页查询
        if (dto.getPage() != null && dto.getSize() != null) {
            // 构造分页
            pageInfo = new Page<>(dto.getPage(), dto.getSize());
        }
        LambdaQueryWrapper<WmNews> queryWrapper = new LambdaQueryWrapper<>();
        // 3.按照标题模糊查询
        if (dto.getTitle() != null) {
            queryWrapper.like(WmNews::getTitle, dto.getTitle());
        }
        // 4.根据状态精准查询
        if (dto.getStatus() != null) {
            queryWrapper.eq(WmNews::getStatus, dto.getStatus());
        }
        // 5.按照创建时间倒叙
        queryWrapper.orderByDesc(WmNews::getCreatedTime);

        // 6.封装作者姓名
        Page<WmNews> page = wmNewsService.page(pageInfo, queryWrapper);
        List<WmNews> wmNews = page.getRecords();
        List<WmNewsDto> data = new ArrayList<>(wmNews.size());
        for (WmNews wmNew : wmNews) {
            WmNewsDto wmNewsDto = new WmNewsDto();
            BeanUtils.copyProperties(wmNew, wmNewsDto);
            // 查询作者姓名
            if (wmNew.getUserId() != null) {
                WmUser wmUser = wmUserService.getById(wmNew.getUserId());
                wmNewsDto.setAuthorName(wmUser.getName());
            }
            data.add(wmNewsDto);
        }

        // 6.结果返回
        ResponseResult responseResult =
                new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(data);
>>>>>>> d85f04899912ec8431d04fc95699de5f6201f34b
        return responseResult;
    }

    /**
     * 查询文章详情
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult getDetails(Integer id) {
<<<<<<< HEAD
        WmNews wmNews = getById(id);
        return new ResponseResult<>(200,wmNews);
=======
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 获取文章
        WmNews wmNews = wmNewsService.getById(id);
        WmNewsDto wmNewsDto = new WmNewsDto();
        // 封装作者名称
        BeanUtils.copyProperties(wmNews, wmNewsDto);
        if (wmNews.getUserId() != null) {
            WmUser wmUser = wmUserService.getById(wmNews.getUserId());
            wmNewsDto.setAuthorName(wmUser.getName());
        }

        // 返回结果
        return ResponseResult.okResult(wmNewsDto);
>>>>>>> d85f04899912ec8431d04fc95699de5f6201f34b
    }

    /**
     * 审核失败
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult fail(RequestParamDto dto) {
        // 拒绝以后需要给出原因，并修改文章的状态为2
        if (dto == null) { //检查数据
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        if (dto.getId() == null) {
            return ResponseResult
                    .errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "文章为空");
        }

        // 根据id查询文章并修改状态
        wmNewsService.update(Wrappers
                .<WmNews>lambdaUpdate()
                .eq(WmNews::getId, dto.getId())
                .set(WmNews::getStatus, WmNews.Status.FAIL)
                .set(WmNews::getReason, dto.getMsg()));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 审核成功
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult pass(RequestParamDto dto) {
        // 需要创建app端的文章信息，并更新自媒体文章的状态
        if (dto == null) { //检查数据
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        if (dto.getId() == null) {
            return ResponseResult
                    .errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "文章为空");
        }

        // 创建app端的文章信息

        // 更新自媒体文章的状态
        wmNewsService.update(Wrappers
                .<WmNews>lambdaUpdate()
                .eq(WmNews::getId, dto.getId())
                .set(WmNews::getStatus, WmNews.Status.SUCCESS));
        return null;
    }
}
